#include "dormitorymanagementpage.h"
#include "ui_dormitorymanagementpage.h"
#include <QMessageBox>
#include <QInputDialog>
#include "sqlhelper.h"

DormitoryManagementPage::DormitoryManagementPage(QWidget* parent)
    : MyWidget(parent)
    , ui(new Ui::DormitoryManagementPage)
{
    ui->setupUi(this);
    loadDormitoryData();

    connect(ui->pushButtonInsert, &QPushButton::clicked, this, &DormitoryManagementPage::on_pushButtonInsert_clicked);
    connect(ui->pushButtonModify, &QPushButton::clicked, this, &DormitoryManagementPage::on_pushButtonModify_clicked);
    connect(ui->pushButtonDelete, &QPushButton::clicked, this, &DormitoryManagementPage::on_pushButtonDelete_clicked);
}

DormitoryManagementPage::~DormitoryManagementPage()
{
    delete ui;
}

void DormitoryManagementPage::loadDormitoryData()
{
    // 获取宿舍数据
    QList<QVariantList> dormitories = SqlHelper::getInstance().queryDormitories();

    ui->tableWidget->clearContents();
    ui->tableWidget->setRowCount(0);

    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "宿舍编号" << "宿舍名称" << "楼层" << "总人数");

    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); // 占据整个宽度

    // 填充数据到表格
    for (const QVariantList &dormitory : dormitories)
    {
        int row = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);

        ui->tableWidget->setItem(row, 0, new QTableWidgetItem(dormitory[0].toString()));
        ui->tableWidget->setItem(row, 1, new QTableWidgetItem(dormitory[1].toString()));
        ui->tableWidget->setItem(row, 2, new QTableWidgetItem(dormitory[2].toString()));
        ui->tableWidget->setItem(row, 3, new QTableWidgetItem(dormitory[3].toString()));
    }
}

void DormitoryManagementPage::on_pushButtonInsert_clicked()
{
    bool ok;
    QString dormitoryNumber = QInputDialog::getText(this, "新增宿舍", "请输入宿舍编号：", QLineEdit::Normal, "", &ok);
    if (!ok || dormitoryNumber.isEmpty())
        return;

    QString dormitoryName = QInputDialog::getText(this, "新增宿舍", "请输入宿舍名称：", QLineEdit::Normal, "", &ok);
    if (!ok || dormitoryName.isEmpty())
        return;

    int floor = QInputDialog::getInt(this, "新增宿舍", "请输入楼层：", 1, 1, 100, 1, &ok);
    if (!ok)
        return;

    int totalPeople = QInputDialog::getInt(this, "新增宿舍", "请输入总人数：", 1, 1, 100, 1, &ok);
    if (!ok)
        return;

    if (SqlHelper::getInstance().insertDormitory(dormitoryNumber, dormitoryName, floor, totalPeople)) {
        QMessageBox::information(this, "成功", "宿舍添加成功！");
        loadDormitoryData();  // 重新加载数据
    } else {
        QMessageBox::critical(this, "错误", "宿舍添加失败！");
    }
}

void DormitoryManagementPage::on_pushButtonModify_clicked()
{
    // 获取选中的行
    int currentRow = ui->tableWidget->currentRow();
    if (currentRow == -1) {
        QMessageBox::warning(this, "选择错误", "请选择一行进行修改");
        return;
    }

    QString dormitoryNumber = ui->tableWidget->item(currentRow, 0)->text();
    QString dormitoryName = ui->tableWidget->item(currentRow, 1)->text();
    int floor = ui->tableWidget->item(currentRow, 2)->text().toInt();
    int totalPeople = ui->tableWidget->item(currentRow, 3)->text().toInt();

    bool ok;
    QString newDormitoryName = QInputDialog::getText(this, "修改宿舍", "请输入新的宿舍名称：", QLineEdit::Normal, dormitoryName, &ok);
    if (!ok || newDormitoryName.isEmpty())
        return;

    int newFloor = QInputDialog::getInt(this, "修改宿舍", "请输入新的楼层：", floor, 1, 100, 1, &ok);
    if (!ok)
        return;

    int newTotalPeople = QInputDialog::getInt(this, "修改宿舍", "请输入新的总人数：", totalPeople, 1, 100, 1, &ok);
    if (!ok)
        return;

    if (SqlHelper::getInstance().updateDormitory(currentRow, dormitoryNumber, newDormitoryName, newFloor, newTotalPeople)) {
        QMessageBox::information(this, "成功", "宿舍修改成功！");
        loadDormitoryData();  // 重新加载数据
    } else {
        QMessageBox::critical(this, "错误", "宿舍修改失败！");
    }
}

void DormitoryManagementPage::on_pushButtonDelete_clicked()
{
    // 获取选中的行
    int currentRow = ui->tableWidget->currentRow();
    if (currentRow == -1) {
        QMessageBox::warning(this, "选择错误", "请选择一行进行删除");
        return;
    }

    QString dormitoryNumber = ui->tableWidget->item(currentRow, 0)->text();
    if (QMessageBox::question(this, "删除宿舍", "确定删除宿舍 " + dormitoryNumber + " 吗？", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
        if (SqlHelper::getInstance().deleteDormitory(currentRow)) {
            QMessageBox::information(this, "成功", "宿舍删除成功！");
            loadDormitoryData();  // 重新加载数据
        } else {
            QMessageBox::critical(this, "错误", "宿舍删除失败！");
        }
    }
}
